<html>
<head>
Sale
</head>
<body>
<form onsubmit="handleSubmit(); return false;">
  

<div class='title'>Sale</div>
<div class='div'>&nbsp</div>
<div class='field'>Date:<input type='text' id='invoicedate' name='invoicedate' ></div>
<div class='field'>Customer:<span id='customerspan' name='customerspan' ></span><a href='./customer.html'>[Manage]</a></div>
<span id='productspan' name='productspan' ></span>

<!--<div class='field'>Ordered:<input type='text' id='ordered' name='ordered' ></div>
<div class='field'>Cost:<input type='text' id='cost' name='cost' ></div>
<div class='field'>Price:<input type='text' id='price' name='price' ></div>
-->
<input type='submit' value='Save' />
</form>
<span id="status">&nbsp;</span>

<!-- ====================================== -->
<!-- End HTML code.  Begin JavaScript code. -->

<script type="text/javascript"  src="../gears_init.js"></script>
<script type="text/javascript" src="sample.js"></script>
<script>

var db;
init();

// Open this page's local database.
function init() {
  var success = false;

  if (window.google && google.gears) {
    try {
      db = google.gears.factory.create('beta.database');

      if (db) {
        db.open('database-demo');
        db.execute('create table if not exists Product' +
                   ' (id int primarykey,' +
                     ' UPC text,' +
                     ' description text,' +
                     ' brand text,' +
                     ' model text,' +
                     ' type text,' +
                     ' quantity real,' +
                     ' ordered real,' +
                     ' cost real,' +
                     ' price text' +
                     ')');

        success = true;
        // Initialize the UI at startup.
        
        displayRecentPhrases();
        loadCustomers();
      }

    } catch (ex) {
      setError('Could not create database: ' + ex.message);
      alert('Could not create database: ' + ex.message);
    }
  }

  // Enable or disable UI elements

  var inputs = document.forms[0].elements;
  for (var i = 0, el; el = inputs[i]; i++) {
    el.disabled = !success;
  }

}

function handleSubmit() {
  if (!google.gears.factory || !db) {
    return;
  }

  var upcT = getElementById('upc');
  var descriptionT = getElementById('description');
  var brandT = getElementById('brand');
  var modelT = getElementById('model');
  var typeT = getElementById('type');
  var quantityT = getElementById('quantity');
  var orderedT = getElementById('ordered');
  var costT = getElementById('cost');
  var priceT = getElementById('price');
  
  var upc = upcT.value;
  var description = descriptionT.value;
  var brand = brandT.value;
  var model = modelT.value;
  var type = typeT.value;
  var quantity = quantityT.value;
  var ordered = orderedT.value;
  var cost = costT.value;
  var price = priceT.value;
  var currTime = new Date().getTime();

  // Insert the new item.
  // The Gears database automatically escapes/unescapes inserted values.
  db.execute('insert into Product (upc, description, brand, model, type, quantity, ordered, cost, price) values (?,?,?,?,?,?,?,?,?)', [upc, description, brand, model, type, quantity, ordered, cost, price]);
	alert('Product added.');
  // Update the UI.
  //elm.value = '';
  displayRecentPhrases();
}


function displayRecentPhrases() {
  var recentPhrases = ['', '', ''];

  // Get the 3 most recent entries. Delete any others.
  var rs = db.execute('select * from customer order by 1 desc');
  var index = 0;
  while (rs.isValidRow()) {
    if (index < 3) {
      recentPhrases[index] = rs.field(0)+'-'+ rs.field(1);
    } else {
      alert('Nothing');//db.execute('delete from customer where Timestamp=?', [rs.field(1)]);
    }
    ++index;
    rs.next();
  }
  rs.close();

  var status = getElementById('status');
  status.innerHTML = '';
  for (var i = 0; i < recentPhrases.length; ++i) {
    var id = 'phrase' + i;
    status.innerHTML += '<span id="' + id + '"></span><br>';
    var bullet = '(' + (i + 1) + ') ';
    setTextContent(getElementById(id), bullet + recentPhrases[i]);
  }
}

function loadProducts() {
  
  var rs = db.execute('select * from Product order by 2 desc');
  var index = 0;
  var ptypespan = getElementById('ptypespan');
  var itext = '';
  itext += '<select name="producttype">';
  while (rs.isValidRow()) {
    itext += '<option value="'+ rs.field(0) +'" >'+ rs.field(1) + '</option>';
    rs.next();
  }
  rs.close();
   itext += '</select>';
   //alert(itext);
   ptypespan.innerHTML = itext;
  
  
}

function loadCustomers() {
  
  var rs = db.execute('select id,firstname from customer order by 2 desc');
  var index = 0;
  var customerspan = getElementById('customerspan');
  var itext = '';
  itext += '<select name="customer">';
  while (rs.isValidRow()) {
    itext += '<option value="'+ rs.field(0) +'" >'+ rs.field(1) + '</option>';
    rs.next();
  }
  rs.close();
   itext += '</select>';
   //alert(itext);
   customerspan.innerHTML = itext;
  
  
}


</script>
</body>

</html>